*******		REPLICATION FILES
*******		Climate Variability and Irregular Migration to the European Union 
*******		Global Environmental Change 
*******		Fabien Cottier and Idean Salehyan
*******		Replication: Sensitivity analysis (S10) splitting sample by GDP per capita (Tables A.33-34, Figure A.25)
*******		This version: April 2021

* note: stata packages required for running models
* - coefplot
* - crossfold
* - plottig


clear all

set more off
set scheme plottig
cd "path/to/replication/directory"

* load data
use "data_quarter.dta", clear 

* load stata risk scripts
qui do scripts/script_rr_spei_gdppc_102020

* duplicates data
duplicates list cowc year quarter
duplicates tag cowc year quarter, gen(_dupl)
duplicates drop cowc year quarter, force

* set time series
sort cowc year quarter
gen quarter_int = real(regexs(1)) if regexm(quarter,"([0-9]+)")
gen quarterS=(year-2005)*4+quarter_int
order cowc nationality year quarter quarter_int quarterS
tsset cowc quarterS
 
* gen additional variables
encode continent, gen(contN)
recode contN (5=.)


* generate lag quartely migration variables
by cowc: gen nmigrq_exbalk_1tl = nmigrq_exbalk[_n-1]
by cowc: gen nmigrq_exbalk_2tl = nmigrq_exbalk[_n-2]
by cowc: gen nmigrq_exbalk_3tl = nmigrq_exbalk[_n-3]
by cowc: gen nmigrq_exbalk_4tl = nmigrq_exbalk[_n-4]
by cowc: gen nmigrq_exbalk_ln_1tl = nmigrq_exbalk_ln[_n-1]
by cowc: gen nmigrq_exbalk_ln_2tl = nmigrq_exbalk_ln[_n-2]
by cowc: gen nmigrq_exbalk_ln_3tl = nmigrq_exbalk_ln[_n-3]
by cowc: gen nmigrq_exbalk_ln_4tl = nmigrq_exbalk_ln[_n-4]


* gen dummies variables for sample splitting: low GDP / high GDP
qui xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* i.year if migr100_exbalk==1, ///
 fe vce(cluster cowc)

sum gdppc if e(sample) & year==2010, d
scalar GDPMed=r(p50)
gen gdppc_L=0  if e(sample) & year==2010
recode gdppc_L (0=1) if gdppc-GDPMed < 0 & year==2010
by cowc: replace gdppc_L = gdppc_L[21] if missing(gdppc_L)

* generate dummies variable for extreme weather (cut-off 10 percentile / 90 percentile)
qui xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* wmean_speiy /// 
 i.year i.quarter_int if migr100_exbalk==1, ///
 fe vce(cluster cowc)

centile (wmean_speiy) if e(sample), centile (10 90)
gen spei_drought = 0 if wmean_speiy!=.
recode spei_drought (0=1) if wmean_speiy <= r(c_1) 
centile (wmean_speiy) if e(sample), centile (10 90)
gen spei_hrain = 0 if wmean_speiy!=.
recode spei_hrain (0=1) if wmean_speiy >=  r(c_2) 


*******		  Macros

global SPEI_Y0 wmean_speiy

global SPEI_SQ_Y0 c.wmean_speiy##c.wmean_speiy
 
global SPEI_Y0Y2 L(0 4 8).wmean_speiy

global SPEI_SQ_Y0Y2 c.wmean_speiy##c.wmean_speiy cL4.wmean_speiy##cL4.wmean_speiy /// 
  cL8.wmean_speiy##cL8.wmean_speiy




*******		 Table A.33


* NULL Model // poor countries
qui xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* $SPEI_Y0 ///
	i.year i.quarter_int if migr100_exbalk==1 & gdppc_L==1, ///
	fe vce(cluster cowc)
qui xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* ///
	i.year i.quarter_int if e(sample), ///
	fe vce(cluster cowc)
est sto tabA33_m0H
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse null model     : " CVrmse
* list of countries
est resto tabA33_m0H
unique cowc if e(sample)
tabulate nationality contN if e(sample)

* NULL Model // rich countries
qui xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* $SPEI_Y0 ///
	i.year i.quarter_int if migr100_exbalk==1 & gdppc_L==0, ///
	fe vce(cluster cowc)
qui xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* ///
	i.year i.quarter_int if e(sample), ///
	fe vce(cluster cowc)
est sto tabA33_m0L
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse null model     : " CVrmse
global CVrmsenull_LA=round(CVrmse,0.001)
* list of countries
est resto tabA33_m0L
unique cowc if e(sample)
tabulate nationality contN if e(sample)


* Model 5 // poor countries
xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* $SPEI_Y0 ///
  i.year i.quarter_int if migr100_exbalk==1 & gdppc_L==1, ///
  fe vce(cluster cowc)
est sto tabA33_m5
* F test spei parameters & aic
test wmean_speiy
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse

* Model 6 // rich countries
xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* $SPEI_Y0 ///
  i.year i.quarter_int  if migr100_exbalk==1 & gdppc_L==0, ///
  fe vce(cluster cowc)
est sto tabA33_m6
* F test spei parameters & aic
test wmean_speiy
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse


* Model 7 // poor countries
xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* $SPEI_SQ_Y0 ///
  i.year i.quarter_int  if migr100_exbalk==1 & gdppc_L==1, ///
  fe vce(cluster cowc)
est sto tabA33_m7
* F test spei parameters & aic
test wmean_speiy c.wmean_speiy#c.wmean_speiy
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse

* Model 8 // rich countries
xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* $SPEI_SQ_Y0 ///
  i.year i.quarter_int  if migr100_exbalk==1 & gdppc_L==0, ///
  fe vce(cluster cowc)
est sto tabA33_m8
* F test spei parameters & aic
test wmean_speiy c.wmean_speiy#c.wmean_speiy
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse


* Model 9 // poor countries
xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* $SPEI_Y0Y2 ///
  i.year i.quarter_int  if migr100_exbalk==1 & gdppc_L==1, ///
  fe vce(cluster cowc)
est sto tabA33_m9
unique cowc if e(sample)
* F test spei parameters & aic
test L0.wmean_speiy L4.wmean_speiy L8.wmean_speiy
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse

* Model 10 // rich countries
xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* $SPEI_Y0Y2 ///
  i.year i.quarter_int  if migr100_exbalk==1 & gdppc_L==0, ///
  fe vce(cluster cowc)
est sto tabA33_m10
unique cowc if e(sample)
* F test spei parameters & aic
test L0.wmean_speiy L4.wmean_speiy L8.wmean_speiy
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse


* Model 11 // poor countriese
xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* $SPEI_SQ_Y0Y2 ///
  i.year i.quarter_int  if migr100_exbalk==1 & gdppc_L==1, ///
  fe vce(cluster cowc)
est sto tabA33_m11
* F test spei parameters & aic
test L0.wmean_speiy L4.wmean_speiy L8.wmean_speiy ///
 cL0.wmean_speiy#cL0.wmean_speiy cL4.wmean_speiy#cL4.wmean_speiy cL8.wmean_speiy#cL8.wmean_speiy
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse

* Model 12 // rich countries
* low reliance on agriculture
xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* $SPEI_SQ_Y0Y2 ///
  i.year i.quarter_int  if migr100_exbalk==1 & gdppc_L==0, ///
  fe vce(cluster cowc)
est sto tabA33_m12
* F test spei parameters & aic
test L0.wmean_speiy L4.wmean_speiy L8.wmean_speiy ///
 cL0.wmean_speiy#cL0.wmean_speiy cL4.wmean_speiy#cL4.wmean_speiy cL8.wmean_speiy#cL8.wmean_speiy
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse



*******		 Table A.34



* Model 13 // poor countries
xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* spei_drought spei_hrain ///
	  i.year i.quarter_int if migr100_exbalk==1 & gdppc_L==1, fe vce(cluster cowc)
est sto tabA34_m13
* F test spei parameters & aic
test spei_drought spei_hrain
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse

* Model 14 // rich countries
xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* spei_drought spei_hrain  ///
  i.year i.quarter_int if migr100_exbalk==1 & gdppc_L==0, fe vce(cluster cowc)
est sto tabA34_m14
* F test spei parameters & aic
test spei_drought spei_hrain
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse


* Model 15 // poor countries
xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* L(0 4 8).spei_drought L(0 4 8).spei_hrain ///
	  i.year i.quarter_int if migr100_exbalk==1 & gdppc_L==1, fe vce(cluster cowc)
est sto tabA34_m15
* F test spei parameters & aic
test L0.spei_drought L4.spei_drought L8.spei_drought L0.spei_hrain L4.spei_hrain L8.spei_hrain
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse

* Model 16 // rich countries
xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* L(0 4 8).spei_drought L(0 4 8).spei_hrain  ///
  i.year i.quarter_int if migr100_exbalk==1 & gdppc_L==0, fe vce(cluster cowc)
est sto tabA34_m16
* F test spei parameters & aic
test L0.spei_drought L4.spei_drought L8.spei_drought L0.spei_hrain L4.spei_hrain L8.spei_hrain
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse



*******		 Relative risk figures



* Figure A.25 (Model 7 & 8, Table A.33)

qui rr_est_Sp tabA33_m7 tabA33_m8 "quadratic"





*******		 Correlation GDP per capita / share of agriculture in labor


qui xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* wmean_speiy ///
 i.year i.quarter_int if migr100_exbalk==1, ///
 fe vce(cluster cowc)
cor agriLab gdppc if e(sample)


